package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"sync"
	"user_srv/appconfig"
)

var (
	Db   *gorm.DB
	err  error
	once sync.Once
)

func InitMySQL() {
	mySQLConfig := appconfig.AppConf.MySQLConfig
	fmt.Println(mySQLConfig)
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", mySQLConfig.User, mySQLConfig.Password, mySQLConfig.Host, mySQLConfig.Port, mySQLConfig.Database)
	once.Do(func() {
		Db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
		if err != nil {
			fmt.Println("MYSQL 连接失败")
			return
		}
	})
	fmt.Println("MYSQL 连接成功")
	Db.AutoMigrate(new(User), new(UserRealName))
}
